#ifndef HTTPTHREAD_H
#define HTTPTHREAD_H

#include <QDir>
#include <QObject>
#include <QThread>
#include <QFileInfo>
#include <QEventLoop>
#include <QNetworkReply>
#include <QHttpMultiPart>
#include <QNetworkAccessManager>

#include "json_utils.h"
#include "project_global.h"

class PROJECT_EXPORT HttpObject : public QObject
{
    Q_OBJECT
public:
    HttpObject(QObject *parent = nullptr);
    ~HttpObject();

public slots:
    int getRequest(QString url);                       // 简单的Get请求
    int postRequest(QString url, QString paramer);     // 简单的post请求
    int postRequestWithFile(QString filePath, QString url);     // 上传文件POST请求
    int downloadFileFromWeb(QString fileUrl, QString&fileSavePath); // 下载文件GET请求

    int normalGetRequest(QString paramer1, QString paramer2);   // 多个独立参数GET请求
    int getRequestWithArray(QVector<QString> m_filePathVector); // 参数数组GET请求
    int normalPostRequest(QString paramer1, QString paramer2);  // 上传参数数据POST请求
    int postRequestWithPara(QString paramer1, QString paramer2,QString filePath);  // 上传参数和文件POST请求
    void downloadDirectoryFromWeb(QString directoryUrl, QString directorySavePath); // 下载文件目录GET请求

signals:
    void sigResult(QString result);                                   // post/get的请求结果
    void sigRequestProgress(qint64 bytesReceived, qint64 bytesTotal); // http请求进度
    void sigRequestResult(bool result);                               // http请求结果
};



class PROJECT_EXPORT HttpThread : public QObject
{
    Q_OBJECT
public:
    HttpThread(QObject *parent = nullptr);
    ~HttpThread();

    void startWorking();
    void stopWorking();

    void getRequest(QString url);                                       // 简单的Get请求
    void postRequest(QString url, QString paramer);                     // 简单的post请求
    void downloadFileFromWeb(QString fileUrl,QString&fileSavePath);     // 文件下载接口
    void uploadFileToWeb(QString filePath, QString url);                // 上传文件接口

signals:
    // object
    void sigGetRequest(QString url);                                    // 简单的Get请求
    void sigPostRequest(QString url, QString paramer);                  // 简单的post请求
    void sigDownloadFile(QString fileUrl,QString&fileSavePath);         // 下载文件
    void sigUploadFile(QString filePath, QString url);                  // 上传文件

    // 对外
    void sigResult(QString result);                                     // post/get的请求结果
    void sigRequestProgress(qint64 bytesReceived, qint64 bytesTotal);   // http请求进度
    void sigRequestResult(bool result);                                 // http请求结果

private:
    HttpObject *http_object;
    QThread m_thread;
};

#endif // HTTPTHREAD_H
